/*
 * $HeadURL: $
 * $Id: $
 * Copyright (c) 2012 by Wodensoft System, all rights reserved.
 */
package com.wodensoft.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Woden
 * 
 */
public class Formatter {

    static Logger logger = LoggerFactory.getLogger(Formatter.class);

    /**
     * 将资源文件中的中文格式化
     * 
     * @param sourceString
     * @return
     */
    public static String fromChinese(String sourceString) {
        if (sourceString == null || "".equals(sourceString)) {
            return "";
        } else {
            String result;
            try {
                result = new String(sourceString.getBytes("iso8859-1"));
            } catch (UnsupportedEncodingException e) {
                logger.error("字符串的中文化异常:Formatter.fromChinese : " + e);
                result = "";
            }
            return result;
        }
    }

    /**
     * 格式化日期成字符串（yyyy-MM-dd）
     * 
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (date == null) {
            return "";
        }
        return dateFormat.format(date);
    }

    /**
     * 格式化日期成字符串（yyyy-MM-dd HH:mm:ss）
     * 
     * @param date
     * @return
     */
    public static String formatDatetime(Date date) {
        return formatDatetime(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化日期成自定义格式字符串
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDatetime(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 格式化价格
     * 
     * @param price
     * @param length
     * @return
     */
    public static String formatPrice(BigDecimal price, String pattern) {
        if (pattern == null || pattern.equals("")) {
            pattern = "#,##0.00##";
        }

        return new java.text.DecimalFormat(pattern).format(price);
    }

    /**
     * 格式化价格
     * 
     * @param price
     * @return
     */
    public static String formatPrice(BigDecimal price) {

        return formatPrice(price, null);
    }

    /**
     * 数字格式化成大写
     * 
     * @param price
     * @return
     */
    public static String formatChinesePrice(BigDecimal price) {

        double n = price.doubleValue();
        String fraction[] = { "角", "分" };
        String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };

        String head = n < 0 ? "负" : "";
        n = Math.abs(n);
        String s = "";

        for (int i = 0; i < fraction.length; i++) {
            s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
        }

        if (s.length() < 1) {
            s = "整";
        }

        int integerPart = (int) Math.floor(n);
        for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
            String p = "";
            for (int j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[integerPart % 10] + unit[1][j] + p;
                integerPart = integerPart / 10;
            }

            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
        }
        return head
                + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零")
                        .replaceAll("^整$", "零元整");

    }
}
